#include "SDL/SDL.h"
#include "palette.h"
#include "video.h"
#include <stdlib.h>



layerpalette foregroundpal;
layerpalette backgroundpal;
long transparent;

word *vRam;                          //tile data

word *fgRam;                         //foreground namespace
word *bgRam;                         //background namespace

word *tRam;  //non-scrolling text data

byte vRamSprites[640];               //sprites

signed long hScrollTable[480];     //horizontal scrolling
long vScrollRam[40];                 //vertical scrolling
bool vScrollFullscreen;              //is only the first index in vsram used to scroll entire screen vertically?

//display buffer, copied to the correct rendering subsystem for immediate display
long *pixels;


void setfcRam(byte line, byte index, word color) { foregroundpal.line[line].color[index]=color; }
void setbcRam(byte line, byte index, word color) { backgroundpal.line[line].color[index]=color; } 
word getfcRam(byte line, byte index) { return foregroundpal.line[line].color[index]; }
word getbcRam(byte line, byte index) { return backgroundpal.line[line].color[index]; }

void setTransparent(long color) { transparent = color; }

word *getvRam() { return vRam; }
word *getfgRam() { return fgRam; }

void initVideo()
{
     fgRam=(word*)malloc(FGRAMSIZE);
     bgRam=(word*)malloc(BGRAMSIZE);
     vRam=(word*)malloc(VRAMSIZE);  
     
     tRam=(word*)malloc(8192); 
     
     pixels=(long*)malloc(DISPLAYWIDTH*DISPLAYHEIGHT*sizeof(long));

     vScrollFullscreen = true;
}

void videoToSurface(SDL_Surface *surface)
{
    
    bgTilesToPixelBuf(PRIORITY_LOW);
    fgTilesToPixelBuf(PRIORITY_LOW);
    spritesToPixelBuf(PRIORITY_LOW);
    bgTilesToPixelBuf(PRIORITY_HIGH);
    fgTilesToPixelBuf(PRIORITY_HIGH);
    spritesToPixelBuf(PRIORITY_HIGH);
    textToPixelBuf();
    
    
    pixelbuftosurface(surface);
}

/* Index + 0  :   ------yy yyyyyyyy
 Index + 2  :   ----hhvv
 Index + 3  :   -lllllll
 Index + 4  :   pccvhnnn nnnnnnnn
 Index + 6  :   ------xx xxxxxxxx

 y = Vertical coordinate of sprite
 h = Horizontal size in cells (00b=1 cell, 11b=4 cells)
 v = Vertical size in cells (00b=1 cell, 11b=4 cells)
 l = Link field
 p = Priority
 c = Color palette
 v = Vertical flip
 h = Horizontal flip
 n = Sprite pattern start index
 x = Horizontal coordinate of sprite*/

#define SPRITEXOFFSET -128    
#define SPRITEYOFFSET -128

void spritesToPixelBuf(int priorityLevel)
{
     //printf("spritesToSurface()...\n");
     
     long colorBuf[4][16];
     for (int line=0; line<4; line++)
     {
         for (int color=0; color<16; color++)
         {
             colorBuf[line][color]=palColor2RGB(getfcRam(line,color));
         }
     }
     
     byte linkField=0;
     
     do //8 byte entrys
     {
         //printf("Sprite: %i\n", linkField+1);
         byte *vRamSpritesPtr=vRamSprites+(linkField*8);
         //word spritey=(*(vRamSpritesPtr))+(*(vRamSpritesPtr+1)<<8);
         word spritey=(*(vRamSpritesPtr)<<8)+(*(vRamSpritesPtr+1));
         vRamSpritesPtr+=2;
         
         byte sizex=((*vRamSpritesPtr & 0xC)>>2)+1; //(00b=1 cell, 11b=4 cells)                        //0x090b = 00001001    0x080b = 00001000
         byte sizey=((*vRamSpritesPtr & 0x3))+1; //Vertical size in cells (00b=1 cell, 11b=4 cells)        right                wrong
         vRamSpritesPtr++;
         
         linkField=(*vRamSpritesPtr & 0x7F);
         vRamSpritesPtr++;
      	 if (linkField==0) { break; }  

         //word patternStartIndex=(*vRamSpritesPtr);         
         //vRamSpritesPtr++;
         //patternStartIndex+=((*vRamSpritesPtr & 0x7))<<8;
	 //byte priority=((*vRamSpritesPtr & 0x80)>>7); 
         //if ( (priorityLevel == 0) ||
         //   ((priorityLevel == 1) && (priority == 1)) || 
         //   ((priorityLevel == 2) && (priority == 0)))
         //{ 
         //byte palLine=((*vRamSpritesPtr & 0x60)>>5);
         //byte flipy=((*vRamSpritesPtr & 0x10)>>4);
         //byte flipx=((*vRamSpritesPtr & 0x8)>>3);
         word patternStartIndex=(*vRamSpritesPtr & 0x7)<<8;    
         byte priority=((*vRamSpritesPtr & 0x80)>>7);                  
                                                                       
         if ( (priorityLevel == 0) ||
              ((priorityLevel == 1) && (priority == 1)) || 
              ((priorityLevel == 2) && (priority == 0)))
         { 
         byte palLine=((*vRamSpritesPtr & 0x60)>>5);
         byte flipy=((*vRamSpritesPtr & 0x10)>>4);
         byte flipx=((*vRamSpritesPtr & 0x8)>>3);   
         vRamSpritesPtr++;
         
         patternStartIndex+=(*vRamSpritesPtr);
         vRamSpritesPtr++;
         
         //word spritex=(*(vRamSpritesPtr))+(*(vRamSpritesPtr+1)<<8); //starts at 128,128
         word spritex=(*(vRamSpritesPtr)<<8)+(*(vRamSpritesPtr+1));       
         vRamSpritesPtr+=2;
         
         /*printf("spritex: %i\n", spritex);
         printf("spritey: %i\n", spritey);
         printf("sizex: %i\n", sizex);
         printf("sizey: %i\n", sizey);
         printf("linkField: %i\n", linkField);
         printf("priority: %i\n", priority);
         printf("palLine: %i\n", palLine);
         printf("flipx: %i\n", flipx);
         printf("flipy: %i\n", flipy);
         printf("patternStartIndex: %i\n",patternStartIndex);*/
         
         long vRamOffset=((patternStartIndex)*16);
         int curTile=0;

         if ((flipx == 0) && (flipy == 0))
         {
	     for (int xlp=0; xlp<sizex; xlp++)
             {
                 int tileX = xlp*8;
                 for (int ylp=0; ylp<sizey; ylp++, curTile++)
                 { 
		 
		     int tileOffset=curTile*16;
                     int tileY = ylp*8;

                     for (int pixy=0; pixy<8; pixy++)
                     {
                         for (int pixx=0; pixx<2; pixx++)
                         {  
                         
			     int pixOffset=(pixx+(pixy*2));
		             long cur4PixVRam=vRamOffset+tileOffset+pixOffset;

                             byte pix1=(vRam[cur4PixVRam]&0xF000)>>12;
                             byte pix2=(vRam[cur4PixVRam]&0x0F00)>>8;
                             byte pix3=(vRam[cur4PixVRam]&0x00F0)>>4;
                             byte pix4=(vRam[cur4PixVRam]&0x000F);
                         
                             long cur4PixX = (spritex + tileX + (pixx*4)) + SPRITEXOFFSET;
                             long cur4PixY = (spritey + tileY + pixy) + SPRITEYOFFSET;
                             if (pix1 != 0) { setpixelbuf(pixels,   cur4PixX, cur4PixY, colorBuf[palLine][pix1], true); }
                             if (pix2 != 0) { setpixelbuf(pixels, cur4PixX+1, cur4PixY, colorBuf[palLine][pix2], true); }
                             if (pix3 != 0) { setpixelbuf(pixels, cur4PixX+2, cur4PixY, colorBuf[palLine][pix3], true); }
                             if (pix4 != 0) { setpixelbuf(pixels, cur4PixX+3, cur4PixY, colorBuf[palLine][pix4], true); }
                    
      		         }
                     }
                 }
             }        
         }
         else if ((flipx == 1) && (flipy == 0))
         {
	     for (int xlp=0; xlp<sizex; xlp++)
             {
                 int tileX = xlp*8;
                 for (int ylp=0; ylp<sizey; ylp++)
                 { 
		     curTile=((sizex-xlp-1)*sizey)+ylp;
		     int tileOffset=curTile*16;
                     int tileY = ylp*8;

                     for (int pixy=0; pixy<8; pixy++)
                     {
                         for (int pixx=0; pixx<2; pixx++)
                         {  
                         
			     int pixOffset=((1-pixx)+(pixy*2));
		             long cur4PixVRam=vRamOffset+tileOffset+pixOffset;

                             byte pix1=(vRam[cur4PixVRam]&0x000F);
                             byte pix2=(vRam[cur4PixVRam]&0x00F0)>>4;
                             byte pix3=(vRam[cur4PixVRam]&0x0F00)>>8;
                             byte pix4=(vRam[cur4PixVRam]&0xF000)>>12;
                         
                             long cur4PixX = (spritex + tileX + (pixx*4)) + SPRITEXOFFSET;
                             long cur4PixY = (spritey + tileY + pixy) + SPRITEYOFFSET;
                             if (pix1 != 0) { setpixelbuf(pixels,   cur4PixX, cur4PixY, colorBuf[palLine][pix1], true); }
                             if (pix2 != 0) { setpixelbuf(pixels, cur4PixX+1, cur4PixY, colorBuf[palLine][pix2], true); }
                             if (pix3 != 0) { setpixelbuf(pixels, cur4PixX+2, cur4PixY, colorBuf[palLine][pix3], true); }
                             if (pix4 != 0) { setpixelbuf(pixels, cur4PixX+3, cur4PixY, colorBuf[palLine][pix4], true); }
                    
      		         }
                     }
                 }
             }        
         }
         else if ((flipx == 0) && (flipy == 1))
         {
	     for (int xlp=0; xlp<sizex; xlp++)
             {
                 int tileX = xlp*8;
                 for (int ylp=0; ylp<sizey; ylp++)
                 { 
		     curTile=(xlp*sizey)+(sizey-ylp-1);
		     int tileOffset=curTile*16;
                     int tileY = ylp*8;

                     for (int pixy=0; pixy<8; pixy++)
                     {
                         for (int pixx=0; pixx<2; pixx++)
                         {  
                         
			     int pixOffset=(pixx+((7-pixy)*2));
		             long cur4PixVRam=vRamOffset+tileOffset+pixOffset;

                             byte pix1=(vRam[cur4PixVRam]&0xF000)>>12;
                             byte pix2=(vRam[cur4PixVRam]&0x0F00)>>8;
                             byte pix3=(vRam[cur4PixVRam]&0x00F0)>>4;
                             byte pix4=(vRam[cur4PixVRam]&0x000F);
                         
                             long cur4PixX = (spritex + tileX + (pixx*4)) + SPRITEXOFFSET;
                             long cur4PixY = (spritey + tileY + pixy) + SPRITEYOFFSET;
                             if (pix1 != 0) { setpixelbuf(pixels,   cur4PixX, cur4PixY, colorBuf[palLine][pix1], true); }
                             if (pix2 != 0) { setpixelbuf(pixels, cur4PixX+1, cur4PixY, colorBuf[palLine][pix2], true); }
                             if (pix3 != 0) { setpixelbuf(pixels, cur4PixX+2, cur4PixY, colorBuf[palLine][pix3], true); }
                             if (pix4 != 0) { setpixelbuf(pixels, cur4PixX+3, cur4PixY, colorBuf[palLine][pix4], true); }
                    
      		         }
                     }
                 }
             }        
         }
         else if ((flipx == 1) && (flipy == 1))
         {
	     for (int xlp=0; xlp<sizex; xlp++)
             {
                 int tileX = xlp*8;
                 for (int ylp=0; ylp<sizey; ylp++)
                 { 
		     curTile=((sizex-xlp-1)*sizey)+(sizey-ylp-1);
		     int tileOffset=curTile*16;
                     int tileY = ylp*8;

                     for (int pixy=0; pixy<8; pixy++)
                     {
                         for (int pixx=0; pixx<2; pixx++)
                         {  
                         
			     int pixOffset=((1-pixx)+((7-pixy)*2));
		             long cur4PixVRam=vRamOffset+tileOffset+pixOffset;

                             byte pix1=(vRam[cur4PixVRam]&0x000F);
                             byte pix2=(vRam[cur4PixVRam]&0x00F0)>>4;
                             byte pix3=(vRam[cur4PixVRam]&0x0F00)>>8;
                             byte pix4=(vRam[cur4PixVRam]&0xF000)>>12;
                         
                             long cur4PixX = (spritex + tileX + (pixx*4)) + SPRITEXOFFSET;
                             long cur4PixY = (spritey + tileY + pixy) + SPRITEYOFFSET;
                             if (pix1 != 0) { setpixelbuf(pixels,   cur4PixX, cur4PixY, colorBuf[palLine][pix1], true); }
                             if (pix2 != 0) { setpixelbuf(pixels, cur4PixX+1, cur4PixY, colorBuf[palLine][pix2], true); }
                             if (pix3 != 0) { setpixelbuf(pixels, cur4PixX+2, cur4PixY, colorBuf[palLine][pix3], true); }
                             if (pix4 != 0) { setpixelbuf(pixels, cur4PixX+3, cur4PixY, colorBuf[palLine][pix4], true); }
                    
      		         }
                     }
                 }
             }        
         }
     
     }    
     } while (linkField!=0);  
     

}

           
     
void bgTile(int index, word id)
{
     bgRam[index]=id;
}
     
void fgTile(int index, word id)
{
     fgRam[index]=id;
}

void textTile(int index, word id)
{
    tRam[index]=id;
}

void textTileExt(int index, word id, word *ram)
{
    ram[index]=id;
}

void fgRamDraw()
{
     for (int y=0; y<THEIGHT; y++)
     {
         
         for (int x=0; x<TWIDTH; x++)
         { 
           //fgTileDraw(x*8, y*8, y*TWIDTH+x,0,0);
         }
     }
} 

void UpdateSpriteTable(byte *buf)
{
     
     
     for (int lp=0; lp<640; lp++)
     {
         vRamSprites[lp]=buf[lp];
        // printf("vRamSprites[%i]=%i\n", lp, buf[lp]);
     }
}
         
void UpdateHScrollTable(long *buf)
{
     for (int lp=0; lp<480; lp++)
     {
         hScrollTable[lp]=buf[lp];
         //printf("hScrollTable[%i]=%i\n", lp, hScrollTable[lp]);
     }
}

void UpdateVScrollFullscreen(long scrollval)
{
     vScrollRam[0] = scrollval;
}

void writeVRAM(byte *buf, word len, word offset)
{
     for (int lp=0; lp<len/2; lp++)
     {
	vRam[lp+(offset/2)]=(buf[lp*2]<<8)+(buf[(lp*2)+1]);
     }
}

void setpixelbuf(long *buf, int x, int y, int color)
{
    if (x>=0 && x<DISPLAYWIDTH && y>=0 && y<DISPLAYHEIGHT)
        buf[x + ( y * DISPLAYWIDTH )] = color;
}
void setpixelbuf(long *buf, int x, int y, int color, bool bigpixel)
{
    if (x>=0 && x<DISPLAYWIDTH/PIXELSIZE && y>=0 && y<DISPLAYHEIGHT/PIXELSIZE)
    {
        for (int ylp=0; ylp<PIXELSIZE; ylp++)
        {
            for (int xlp=0; xlp<PIXELSIZE; xlp++)
                buf[((x*PIXELSIZE)+xlp) + ( ((y*PIXELSIZE)+ylp) * DISPLAYWIDTH )] = color;
        }
    }
}

void setpixelbuf(long *buf, int x, int y, int color, int width, int height)
{
    if (x>=0 && x<width && y>=0 && y<height)
        buf[x + ( y * width )] = color;
}
void setpixelbuf(long *buf, int x, int y, int color, int width, int height, bool bigpixel)
{
    if (x>=0 && x<width/PIXELSIZE && y>=0 && y<height/PIXELSIZE)
    {
        for (int ylp=0; ylp<PIXELSIZE; ylp++)
        {
            for (int xlp=0; xlp<PIXELSIZE; xlp++)
                buf[((x*PIXELSIZE)+xlp) + ( ((y*PIXELSIZE)+ylp) * width )] = color;
        }
    }
}





void buftosurface(long *buf, SDL_Surface *surface)
{     
     int *pixels = (int *)surface->pixels; 
     for (int lp=0; lp<DISPLAYSIZE; lp++) 
         pixels[lp] = buf[lp]; 
}

void pixelbuftosurface(SDL_Surface *surface)
{
     
     buftosurface(pixels, surface);
     long clearColor = palColor2RGB(getfcRam(2,0));
     for (int lp=0; lp<DISPLAYSIZE; lp++)
         pixels[lp]=clearColor;
}


            
void setpixelscale(int size, SDL_Surface *surface, int x, int y, int color)
{
     int xsize=size;
     int ysize=size;
     
     
     for (int ylp=0; ylp<ysize; ylp++)
     {
         for (int xlp=0; xlp<xsize; xlp++)
             put_pixel32(surface, (x*xsize)+xlp, (y*ysize)+ylp, color);
     }
}
void setpixelscale(int size, SDL_Surface *surface, int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
     int xsize=size;
     int ysize=size;
     
     
     for (int ylp=0; ylp<ysize; ylp++)
     {
         for (int xlp=0; xlp<xsize; xlp++)
             setpixel(surface, (x*xsize)+xlp, (y*ysize)+ylp, r, g, b);
     }
}
void setpixel(SDL_Surface *surface, int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
    Uint32 *pixmem32;
    Uint32 colour;  
 
    colour = SDL_MapRGB( surface->format, r, g, b );
  
  
    pixmem32 = (Uint32*) surface->pixels  + ((y*surface->w) + (x));
    *pixmem32 = colour;
}



void vRamToSurface(SDL_Surface* surface)
{
     long colorBuf[4][16];
     for (int line=0; line<4; line++)
     {
         for (int color=0; color<16; color++)
         {
             colorBuf[line][color]=palColor2RGB(getfcRam(line,color));
         }
     }
}

extern bool noscrollx, noscrolly;

void fgTilesToPixelBuf(int priorityLevel)
{
     int tilex, tiley;
     int destx, desty;
     int destxprev, destyprev;
     int tilexprev, tileyprev;
          
     long colorBuf[4][16];
     for (int line=0; line<4; line++)
     {
         for (int color=0; color<16; color++)
         {
             colorBuf[line][color]=palColor2RGB(getfcRam(line,color));
         }
     }
     
     //for (int y=0; y<THEIGHT; y++)
     for (int y=0; y<THEIGHT; y++)
     {

         
         //destrect.y=y*8*3;
         tiley=(y*8);
         
         for (int x=0; x<TWIDTH; x++)
         {  
                //destrect.x=((x*8)-hscrollpixoffset)*3;
                tilex=x*8;
                

                //SDL_Surface *flippedTile = SDL_CreateRGBSurface( SDL_SWSURFACE, tile->w, tile->h, tile->format->BitsPerPixel, tile->format->Rmask, tile->format->Gmask, tile->format->Bmask, tile->format->Amask ); 
                            
                //todo: tile 
                //Go through columns 
               // SDL_SetColorKey(scr, SDL_SRCCOLORKEY, palColor2RGB(foregroundpal.line[line].color[0]));
                //SDL_SetColorKey(tile, SDL_SRCCOLORKEY, palColor2RGB(foregroundpal.line[line].color[0]));

                
                for( int ylp = 0, ry = 7; ylp < 8; ylp++, ry-- ) 
                { 
                     
                    signed short hscrollpixoffset=((hScrollTable[((tiley+ylp))]&0xFFFF0000)>>16) & 0x7;
                    signed short hscrolltileoffset=(((hScrollTable[((tiley+ylp))]&0xFFFF0000)>>16) & 0x3F8)>>3;
                    signed short vscrollpixoffset=((vScrollRam[0] & 0xFFFF0000) >> 16) & 0x7;
                    signed short vscrolltileoffset=(((vScrollRam[0] & 0xFFFF0000) >> 16) & 0x3F8)>>3;
                    
                    
                    if (noscrollx) { hscrolltileoffset = 0; hscrollpixoffset = 0; }
                    if (noscrolly) { vscrolltileoffset = 0; vscrollpixoffset = 0; }

                    signed short xIndexWithScroll = (x + hscrolltileoffset);
                    xIndexWithScroll = xIndexWithScroll % TWIDTH;
                    signed short yIndexWithScroll = (y + vscrolltileoffset);
                    yIndexWithScroll = yIndexWithScroll % THEIGHT;

                    

                    word fgRamIndex=(xIndexWithScroll)+(yIndexWithScroll*TWIDTH);
                    word fgRamIndexPrev=fgRamIndex+1;
                    hscrollpixoffset=-hscrollpixoffset;
                    vscrollpixoffset=-vscrollpixoffset;
                    
                    word fgRamVal=fgRam[fgRamIndex];
                    word fgRamValPrev=fgRam[fgRamIndexPrev];
               
                    word flipx=(fgRamVal&0x800)>>11;
                    word flipxPrev=(fgRamValPrev&0x800)>>11;
                    word flipy=(fgRamVal&0x1000)>>12;
                    word flipyPrev=(fgRamValPrev&0x1000)>>12;
                    word line=(fgRamVal&0x6000)>>13;
                    word linePrev=(fgRamValPrev&0x6000)>>13;
                    word priority=(fgRamVal&0x8000)>>15;

                    if ( (priorityLevel == 0) ||
                    ((priorityLevel == 1) && (priority == 1)) || 
                    ((priorityLevel == 2) && (priority == 0)))
                    { 
               
                    word tileindex=(fgRamVal&0x7FF);
                    word tileindexPrev=(fgRamValPrev&0x7FF);
                    
                    long vRamOffset=((tileindex)*16);
                    long vRamOffsetPrev=((tileindexPrev)*16);
                    //SDL_Surface *tile=vRamTiles[tileindex];
                    //SDL_Surface *tileprev=vRamTiles[tileindexPrev];

                    
                    for( int xlp = 0, rx = 7; xlp < 8; xlp++, rx-- ) 
                    { 
                         //Go through rows 

                            //Get pixel 
                            //int pixel = get_pixel32( tile, xlp, ylp); 
                            //int prevpixel = get_pixel32( tileprev, xlp, ylp); 
                            byte curnyb = 3-(xlp%4);
                            byte nybshift = (4*curnyb);
                            word curnybVal = (0xF)<<nybshift;
                            
                            word pixel = (vRam[vRamOffset+(xlp/4)+(ylp*2)] & curnybVal)>>nybshift;
                            word prevpixel = (vRam[vRamOffsetPrev+(xlp/4)+(ylp*2)] & curnybVal)>>nybshift;
                                
                            //Copy pixel 
                            if( ( flipy==1 ) && ( flipx==1 ) )
                            {
                                desty=tiley+ry+(vscrollpixoffset);
                                destx=tilex+rx+(hscrollpixoffset);
                                
                                if (desty>=0&&destx>=0&&destx<=960&&desty<=720 && ((pixel&0x000F)!=0))
                                {
                                   setpixelbuf(pixels, destx, desty, colorBuf[line][pixel&0x000F], true);
                                }
                                if (x==0)
                                {
                                    destxprev=(hscrollpixoffset)-rx;
                                    destyprev=(vscrollpixoffset)-desty;  
                                    if (destyprev>=0&&destxprev>=0&&destxprev<=960&&destyprev<=720&& ((prevpixel&0x000F)!=0))
                                    {
                                        //setpixelbuf(pixels, destxprev, destyprev, colorBuf[line][prevpixel&0x000F], true);
                                    }
                                }
                            }
                            else 
                            {
                                 if( flipx==1 ) 
                                 {
                                     desty=tiley+ylp+(vscrollpixoffset);
                                     destx=tilex+rx+(hscrollpixoffset);
                                     destxprev=(hscrollpixoffset)-rx;
                                     destyprev=(vscrollpixoffset)-desty;  
                                     if (desty>0&&destx>0&&destx<960&&desty<720 && ((pixel&0x000F)!=0))
                                     {
                                         setpixelbuf(pixels, destx, desty, colorBuf[line][pixel&0x000F], true);
                                     }
                                     if (x==0)
                                     {
                                         if (destyprev>=0&&destxprev>=0&&destxprev<=960&&destyprev<=720&& ((prevpixel&0x000F)!=0))
                                         {
                                             //setpixelbuf(pixels, destxprev, destyprev, colorBuf[line][prevpixel&0x000F], true);
                                         }
                                     }
                                 }
                                 else if( flipy==1 )
                                 { 
                                     desty=tiley+ry+(vscrollpixoffset);
                                     destx=tilex+xlp+(hscrollpixoffset);
                                     destxprev=(hscrollpixoffset)-xlp;
                                     destyprev=(vscrollpixoffset)-desty;
                                       
                                     if (desty>=0&&destx>=0&&destx<=960&&desty<=720 && ((pixel&0x000F)!=0))
                                     {
                                         setpixelbuf(pixels, destx, desty, colorBuf[line][pixel&0x000F], true);
                                    }
                                    if (x==0)
                                    {
                                         if (destyprev>=0&&destxprev>=0&&destxprev<=960&&destyprev<=720&& ((prevpixel&0x000F)!=0))
                                         {
                                             //setpixelbuf(pixels, destxprev, destyprev, colorBuf[line][prevpixel&0x000F], true);
                                         }
                                    }
                                 }
                                 else
                                 { 
                                     desty=tiley+ylp+(vscrollpixoffset);
                                     destx=tilex+xlp+(hscrollpixoffset);
                                     //put_pixel32( flippedTile, xlp, ylp, colorBuf[line][pixel&0x000F]);
                                     if (desty>=0&&destx>=0&&destx<=960&&desty<=720 && ((pixel&0x000F)!=0))
                                     {
                                        //put_pixel32(scr,destx,desty,colorBuf[line][pixel&0x000F]);
                                        setpixelbuf(pixels, destx, desty, colorBuf[line][pixel&0x000F], true);
                                     }
                                     if (x==0)
                                     {
                                        destxprev=(hscrollpixoffset)-xlp;
                                        destyprev=(vscrollpixoffset)-desty;
                                        if (destyprev>=0&&destxprev>=0&&destxprev<=960&&destyprev<=720&& ((prevpixel&0x000F)!=0))
                                        {
                                             //put_pixel32(scr,destxprev,destyprev,colorBuf[line][pixel&0x000F]);
                                             //setpixelbuf(pixels, destxprev, destyprev, colorBuf[line][prevpixel&0x000F], true);
                                        }
                                     }
                                     
                                     //if (destyprev>0&&destxprev>0&&destxprev<960&&destyprev<720)
                                     //put_pixel32(scr,destxprev,destyprev,colorBuf[line][prevpixel&0x000F]);
                                 }
                            }
                         }
                    }

                    }

         }
     }  
               
}


void bgTilesToPixelBuf(int priorityLevel)
{
     //SDL_Rect screenrect;
     //screenrect.x=0;
     //screenrect.y=0;
     //screenrect.w=scr->w;
     //screenrect.h=scr->h;
     //SDL_FillRect(scr, &screenrect, 0);
     
     //SDL_Rect destrect;
     int tilex, tiley;
     int destx, desty;
     int destxprev, destyprev;
     int tilexprev, tileyprev;
     
     //todo: v/bfgRamTiles shouldnt even exist here honestly,
     //v/bfgRamTiles is equivalent to the way vRam is being used and could easily be replaced
     //only 64x64 (1024) 8x8 tiles to be displayed onscreen per blit
     //tile would then have to refer to vRam[32*tileindex] instead of vRamTiles[tileindex]
     //a function shall be written that transfers from the pixels in a vRam tile using the specified pallette line and flipx/flipy in an fgRam index
     //to a temporary tile surface which can then be blitted to screen, or just copy directly to screen
     
     long colorBuf[4][16];
     for (int line=0; line<4; line++)
     {
         for (int color=0; color<16; color++)
         {
             colorBuf[line][color]=palColor2RGB(getfcRam(line,color));
         }
     }
     
     //for (int y=0; y<THEIGHT; y++)
     for (int y=0; y<THEIGHT; y++)
     {

         tiley=(y*8);


         
         for (int x=0; x<TWIDTH; x++)
         {  
                //destrect.x=((x*8)-hscrollpixoffset)*3;
                tilex=x*8;
                

                //SDL_Surface *flippedTile = SDL_CreateRGBSurface( SDL_SWSURFACE, tile->w, tile->h, tile->format->BitsPerPixel, tile->format->Rmask, tile->format->Gmask, tile->format->Bmask, tile->format->Amask ); 
                            
                //todo: tile 
                //Go through columns 
                //SDL_SetColorKey(scr, SDL_SRCCOLORKEY, palColor2RGB(foregroundpal.line[2].color[0]));
                //SDL_SetColorKey(tile, SDL_SRCCOLORKEY, palColor2RGB(foregroundpal.line[2].color[0]));

                
                for( int ylp = 0, ry = 7; ylp < 8; ylp++, ry-- ) 
                { 

                    signed short vscrolloffset = vScrollRam[0] & 0x0000FFFF;
                    signed short vscrollpixoffset = vscrolloffset & 0x7;
                    signed short vscrolltileoffset = (vscrolloffset & 0x3F8) >> 3;
                    int hscrollindex = tiley + ylp - (vscrollpixoffset);
 
                    signed short hscrolloffset = (hScrollTable[hscrollindex] & 0x0000FFFF);
                    //hscrolloffset = hscrolloffset % 512;                                      //scroll value will wrap after 512
                    //hscrolloffset = hscrolloffset % -512;
                    signed short hscrollpixoffset=hscrolloffset & 0x7;                        //then grab pixel offset
                    signed short hscrolltileoffset=(hscrolloffset & 0x3F8)>>3;                //and tile offset individually
                    

                    if (noscrollx) { hscrolltileoffset = 0; hscrollpixoffset = 0; }
                    if (noscrolly) { vscrolltileoffset = 0; vscrollpixoffset = 0; }

                    signed short xIndexWithScroll;
                    if (hscrolloffset >= 0)
                    {
                        //hscrollpixoffset = -hscrollpixoffset;
                        xIndexWithScroll = (x - hscrolltileoffset) + (TWIDTH*2);
                    }
                    if (hscrolloffset < 0)
                    {
                        hscrollpixoffset = -(8-hscrollpixoffset);
                        xIndexWithScroll = (x - hscrolltileoffset - 1) + (TWIDTH*2);
                    }
                    xIndexWithScroll = xIndexWithScroll % TWIDTH;

                    signed short yIndexWithScroll = (y + vscrolltileoffset);
                    yIndexWithScroll = yIndexWithScroll % THEIGHT;

                    word bgRamIndex=(xIndexWithScroll)+(yIndexWithScroll*TWIDTH);
                    
                    vscrollpixoffset=-vscrollpixoffset;
                    
                    word bgRamVal=bgRam[bgRamIndex];
               
                    word flipx=(bgRamVal&0x800)>>11;
                    word flipy=(bgRamVal&0x1000)>>12;
                    word line=(bgRamVal&0x6000)>>13;
                    word priority=(bgRamVal&0x8000)>>15;
               
                    if ( (priorityLevel == 0) ||
                    ((priorityLevel == 1) && (priority == 1)) || 
                    ((priorityLevel == 2) && (priority == 0)))
                    { 
                    word tileindex=(bgRamVal&0x7FF);
                    
                    long vRamOffset=((tileindex)*16);
                    //SDL_Surface *tile=vRamTiles[tileindex];
                    //SDL_Surface *tileprev=vRamTiles[tileindexPrev];

                    
                    for( int xlp = 0, rx = 7; xlp < 8; xlp++, rx-- ) 
                    { 
                         //Go through rows 

                            //Get pixel 
                            //int pixel = get_pixel32( tile, xlp, ylp); 
                            //int prevpixel = get_pixel32( tileprev, xlp, ylp); 
                            byte curnyb = 3-(xlp%4);
                            byte nybshift = (4*curnyb);
                            word curnybVal = (0xF)<<nybshift;
                            
                            word pixel = (vRam[vRamOffset+(xlp/4)+(ylp*2)] & curnybVal)>>nybshift;
                                
                            //Copy pixel 
                            if( ( flipy==1 ) && ( flipx==1 ) )
                            {
                                desty=tiley+ry+(vscrollpixoffset);
                                destx=tilex+rx+(hscrollpixoffset);
                                setpixelbuf(pixels, destx, desty, colorBuf[line][pixel&0x000F], true);

                            }
                            else 
                            {
                                 if( flipx==1 ) 
                                 {
                                     desty=tiley+ylp+(vscrollpixoffset);
                                     destx=tilex+rx+(hscrollpixoffset);
                                     setpixelbuf(pixels, destx, desty, colorBuf[line][pixel&0x000F], true);

                                 }
                                 else if( flipy==1 )
                                 { 
                                     desty=tiley+ry+(vscrollpixoffset);
                                     destx=tilex+xlp+(hscrollpixoffset);
                                     setpixelbuf(pixels, destx, desty, colorBuf[line][pixel&0x000F], true);

                                 }
                                 else
                                 { 
                                     desty=tiley+ylp+(vscrollpixoffset);
                                     destx=tilex+xlp+(hscrollpixoffset);
                                     setpixelbuf(pixels, destx, desty, colorBuf[line][pixel&0x000F], true);

                                 }
                            }
                         }
                    }
                    }
         }
     }  
                 
}

void textToPixelBuf()
{

     int tilex, tiley;
     int destx, desty;
     int destxprev, destyprev;
     int tilexprev, tileyprev;
     
     //todo: v/bfgRamTiles shouldnt even exist here honestly,
     //v/bfgRamTiles is equivalent to the way vRam is being used and could easily be replaced
     //only 64x64 (1024) 8x8 tiles to be displayed onscreen per blit
     //tile would then have to refer to vRam[32*tileindex] instead of vRamTiles[tileindex]
     //a function shall be written that transfers from the pixels in a vRam tile using the specified pallette line and flipx/flipy in an fgRam index
     //to a temporary tile surface which can then be blitted to screen, or just copy directly to screen
     
     long colorBuf[4][16];
     for (int line=0; line<4; line++)
     {
         for (int color=0; color<16; color++)
         {
             colorBuf[line][color]=palColor2RGB(getfcRam(line,color));
         }
     }
     
     //for (int y=0; y<THEIGHT; y++)
     for (int y=0; y<THEIGHT; y++)
     {

         
         //destrect.y=y*8*3;
         tiley=(y*8);
         
         for (int x=0; x<TWIDTH; x++)
         {  
                //destrect.x=((x*8)-hscrollpixoffset)*3;
                tilex=x*8;
                

                //SDL_Surface *flippedTile = SDL_CreateRGBSurface( SDL_SWSURFACE, tile->w, tile->h, tile->format->BitsPerPixel, tile->format->Rmask, tile->format->Gmask, tile->format->Bmask, tile->format->Amask ); 
                            
                //todo: tile 
                //Go through columns 
               // SDL_SetColorKey(scr, SDL_SRCCOLORKEY, palColor2RGB(foregroundpal.line[line].color[0]));
                //SDL_SetColorKey(tile, SDL_SRCCOLORKEY, palColor2RGB(foregroundpal.line[line].color[0]));

                
                for( int ylp = 0, ry = 7; ylp < 8; ylp++, ry-- ) 
                { 
                     
                    word tRamIndex=(x)+(y*TWIDTH);
                    word tRamVal=tRam[tRamIndex];
               
                    word flipx=(tRamVal&0x800)>>11;
                    word flipy=(tRamVal&0x1000)>>12;
                    word line=(tRamVal&0x6000)>>13;
                    word priority=(tRamVal&0x8000)>>15;
               
                    word tileindex=(tRamVal&0x7FF);
                    
                    long vRamOffset=((tileindex)*16);
                    //SDL_Surface *tile=vRamTiles[tileindex];
                    //SDL_Surface *tileprev=vRamTiles[tileindexPrev];

                    
                    for( int xlp = 0, rx = 7; xlp < 8; xlp++, rx-- ) 
                    { 
                         //Go through rows 

                            //Get pixel 
                            //int pixel = get_pixel32( tile, xlp, ylp); 
                            //int prevpixel = get_pixel32( tileprev, xlp, ylp); 
                            byte curnyb = 3-(xlp%4);
                            byte nybshift = (4*curnyb);
                            word curnybVal = (0xF)<<nybshift;
                            
                            word pixel = (vRam[vRamOffset+(xlp/4)+(ylp*2)] & curnybVal)>>nybshift;
                                
                            //Copy pixel 
                            if( ( flipy==1 ) && ( flipx==1 ) )
                            {
                                desty=tiley+ry;
                                destx=tilex+rx;
                                
                                if (desty>=0&&destx>=0&&destx<=960&&desty<=720 && ((pixel&0x000F)!=0))
                                {
                                   setpixelbuf(pixels, destx, desty, colorBuf[line][pixel&0x000F], true);
                                }
                            }
                            else 
                            {
                                 if( flipx==1 ) 
                                 {
                                     desty=tiley+ylp;
                                     destx=tilex+rx;
                                  
                                     if (desty>0&&destx>0&&destx<960&&desty<720 && ((pixel&0x000F)!=0))
                                     {
                                         setpixelbuf(pixels, destx, desty, colorBuf[line][pixel&0x000F], true);
                                     }
                                
                                 }
                                 else if( flipy==1 )
                                 { 
                                     desty=tiley+ry;
                                     destx=tilex+xlp;
                                     
                                     if (desty>=0&&destx>=0&&destx<=960&&desty<=720 && ((pixel&0x000F)!=0))
                                     {
                                         setpixelbuf(pixels, destx, desty, colorBuf[line][pixel&0x000F], true);
                                     }
                                  
                                 }
                                 else
                                 { 
                                     desty=tiley+ylp;
                                     destx=tilex+xlp;
                                     //put_pixel32( flippedTile, xlp, ylp, colorBuf[line][pixel&0x000F]);
                                     if (desty>=0&&destx>=0&&destx<=960&&desty<=720 && ((pixel&0x000F)!=0))
                                     {
                                        //put_pixel32(scr,destx,desty,colorBuf[line][pixel&0x000F]);
                                        setpixelbuf(pixels, destx, desty, colorBuf[line][pixel&0x000F], true);
                                     }
                                    
                                     
                                     //if (destyprev>0&&destxprev>0&&destxprev<960&&destyprev<720)
                                     //put_pixel32(scr,destxprev,destyprev,colorBuf[line][prevpixel&0x000F]);
                                 }
                            }
                         }
                    }

         }
     }  
   
}

void textToPixelBuf(word *ram, long *buf, int width, int height)
{

     int tilex, tiley;
     int destx, desty;
     int destxprev, destyprev;
     int tilexprev, tileyprev;
     
     //todo: v/bfgRamTiles shouldnt even exist here honestly,
     //v/bfgRamTiles is equivalent to the way vRam is being used and could easily be replaced
     //only 64x64 (1024) 8x8 tiles to be displayed onscreen per blit
     //tile would then have to refer to vRam[32*tileindex] instead of vRamTiles[tileindex]
     //a function shall be written that transfers from the pixels in a vRam tile using the specified pallette line and flipx/flipy in an fgRam index
     //to a temporary tile surface which can then be blitted to screen, or just copy directly to screen
     
     long colorBuf[4][16];
     for (int line=0; line<4; line++)
     {
         for (int color=0; color<16; color++)
         {
             colorBuf[line][color]=palColor2RGB(getfcRam(line,color));
         }
     }
     
     //for (int y=0; y<THEIGHT; y++)
     for (int y=0; y<THEIGHT; y++)
     {

         
         //destrect.y=y*8*3;
         tiley=(y*8);
         
         for (int x=0; x<TWIDTH; x++)
         {  
                //destrect.x=((x*8)-hscrollpixoffset)*3;
                tilex=x*8;
                

                //SDL_Surface *flippedTile = SDL_CreateRGBSurface( SDL_SWSURFACE, tile->w, tile->h, tile->format->BitsPerPixel, tile->format->Rmask, tile->format->Gmask, tile->format->Bmask, tile->format->Amask ); 
                            
                //todo: tile 
                //Go through columns 
               // SDL_SetColorKey(scr, SDL_SRCCOLORKEY, palColor2RGB(foregroundpal.line[line].color[0]));
                //SDL_SetColorKey(tile, SDL_SRCCOLORKEY, palColor2RGB(foregroundpal.line[line].color[0]));

                
                for( int ylp = 0, ry = 7; ylp < 8; ylp++, ry-- ) 
                { 
                     
                    word tRamIndex=(x)+(y*TWIDTH);
                    word tRamVal=ram[tRamIndex];
               
                    word flipx=(tRamVal&0x800)>>11;
                    word flipy=(tRamVal&0x1000)>>12;
                    word line=(tRamVal&0x6000)>>13;
                    word priority=(tRamVal&0x8000)>>15;
               
                    word tileindex=(tRamVal&0x7FF);
                    
                    long vRamOffset=((tileindex)*16);
                    //SDL_Surface *tile=vRamTiles[tileindex];
                    //SDL_Surface *tileprev=vRamTiles[tileindexPrev];

                    
                    for( int xlp = 0, rx = 7; xlp < 8; xlp++, rx-- ) 
                    { 
                         //Go through rows 

                            //Get pixel 
                            //int pixel = get_pixel32( tile, xlp, ylp); 
                            //int prevpixel = get_pixel32( tileprev, xlp, ylp); 
                            byte curnyb = 3-(xlp%4);
                            byte nybshift = (4*curnyb);
                            word curnybVal = (0xF)<<nybshift;
                            
                            word pixel = (vRam[vRamOffset+(xlp/4)+(ylp*2)] & curnybVal)>>nybshift;
                                
                            //Copy pixel 
                            if( ( flipy==1 ) && ( flipx==1 ) )
                            {
                                desty=tiley+ry;
                                destx=tilex+rx;
                                
                                if (desty>=0&&destx>=0&&destx<=960&&desty<=720 && ((pixel&0x000F)!=0))
                                {
                                   setpixelbuf(buf, destx, desty, colorBuf[line][pixel&0x000F], width, height, true);
                                }
                            }
                            else 
                            {
                                 if( flipx==1 ) 
                                 {
                                     desty=tiley+ylp;
                                     destx=tilex+rx;
                                  
                                     if (desty>0&&destx>0&&destx<960&&desty<720 && ((pixel&0x000F)!=0))
                                     {
                                         setpixelbuf(buf, destx, desty, colorBuf[line][pixel&0x000F], width, height, true);
                                     }
                                
                                 }
                                 else if( flipy==1 )
                                 { 
                                     desty=tiley+ry;
                                     destx=tilex+xlp;
                                     
                                     if (desty>=0&&destx>=0&&destx<=960&&desty<=720 && ((pixel&0x000F)!=0))
                                     {
                                         setpixelbuf(buf, destx, desty, colorBuf[line][pixel&0x000F], width, height, true);
                                     }
                                  
                                 }
                                 else
                                 { 
                                     desty=tiley+ylp;
                                     destx=tilex+xlp;
                                     //put_pixel32( flippedTile, xlp, ylp, colorBuf[line][pixel&0x000F]);
                                     if (desty>=0&&destx>=0&&destx<=960&&desty<=720 && ((pixel&0x000F)!=0))
                                     {
                                        //put_pixel32(scr,destx,desty,colorBuf[line][pixel&0x000F]);
                                        setpixelbuf(buf, destx, desty, colorBuf[line][pixel&0x000F], width, height, true);
                                     }
                                    
                                     
                                     //if (destyprev>0&&destxprev>0&&destxprev<960&&destyprev<720)
                                     //put_pixel32(scr,destxprev,destyprev,colorBuf[line][prevpixel&0x000F]);
                                 }
                            }
                         }
                    }

         }
     }  
   
}

int get_pixel32( SDL_Surface *surface, int x, int y )
{ 
       //Convert the pixels to 32 bit 
       int *pixels = (int *)surface->pixels; 
       
       //Get the requested pixel 
       return pixels[ ( y * surface->w ) + x ]; 
}
void put_pixel32( SDL_Surface *surface, int x, int y, int pixel ) 
{ 
     if (x>=0 && x<960 && y>=0 && y<720)
     {
     //Convert the pixels to 32 bit 
     int *pixels = (int *)surface->pixels; 
     
     //Set the pixel 
     pixels[ ( y * surface->w ) + x ] = pixel; 
     }
     
} 

void SDL_DrawRect(SDL_Surface *super, Sint16 x, Sint16 y, Uint16 w, Uint16 h, Uint32 color)
{
     SDL_LockSurface(super);
     for (int ylp=y; ylp<y+h; ylp++)
     {
     for (int xlp=x; xlp<x+w; xlp++)
     {
         if (ylp==y || ylp==y+h-1)
         setpixelscale(PIXELSIZE,super,xlp,ylp,color);
         else
         {
             if (xlp==x || xlp==x+w-1)
                setpixelscale(PIXELSIZE,super,xlp,ylp,color);
         }
     }
     }
     SDL_UnlockSurface(super);
}
     /*
     //SDL_Rect screenrect;
     //screenrect.x=0;
     //screenrect.y=0;
     //screenrect.w=scr->w;
     //screenrect.h=scr->h;
     //SDL_FillRect(scr, &screenrect, 0);
     
     int tilex, tiley;
     int destx, desty;
     int destxprev, destyprev;
     int tilexprev, tileyprev;
     
     long colorBuf[4][16];
     for (int line=0; line<4; line++)
     {
         for (int color=0; color<16; color++)
         {
             colorBuf[line][color]=palColor2RGB(getfcRam(line,color));
         }
     }
     
     //SDL_SetColorKey(scr, SDL_SRCCOLORKEY, colorBuf[0][0]);

     for (int y=0; y<THEIGHT; y++)
     {
         //destrect.y=y*8*3;
         tiley=(y*8);

         for (int x=0; x<TWIDTH; x++)
         {  
                //destrect.x=((x*8)-hscrollpixoffset)*3;
                tilex=x*8;
                                            
                //todo: tile 
                //Go through columns 
               // SDL_SetColorKey(scr, SDL_SRCCOLORKEY, palColor2RGB(foregroundpal.line[line].color[0]));
                //SDL_SetColorKey(tile, SDL_SRCCOLORKEY, palColor2RGB(foregroundpal.line[line].color[0]));

                
                for( int ylp = 0, ry = 7; ylp < 8; ylp++, ry-- ) 
                { 
                     
     
                    word hscrollpixoffset=hScrollTable[((tiley+ylp))]&0xFFFF0000 & 0x7;
                    word hscrolltileoffset=(hScrollTable[((tiley+ylp))]&0xFFFF0000 & 0x3F8)>>3;
                    
               
                    
                    word fgRamVal=fgRam[((x-hscrolltileoffset)%TWIDTH)+(y*TWIDTH)];
                
                    word flipx=(fgRamVal&0x800)>>11;
                    word flipy=(fgRamVal&0x1000)>>12;
                    word line=(fgRamVal&0x6000)>>13;
                    word priority=(fgRamVal&0x8000)>>15;
                    word tileindex=(fgRamVal&0x7FF);
                    //tileindex=tileindex-(tileindex&0x100);
     
                    //SDL_Surface *tile=vRamTiles[tileindex];
                    //if (tileindex%64==0) { tileindex+=65; }
                    //SDL_Surface *tileprev=vRamTiles[tileindex-1];
                    int previndex=tileindex;
                    if ((tileindex%64)==0) { previndex+=64; }
                    else { previndex-=1; }

                    long vRamOffset=((tileindex)*16);
                    long vRamOffsetPrev=((previndex)*16);
                    

                    for( int xlp = 0, rx = 7; xlp < 8; xlp++, rx-- ) 
                    { 
                         //Go through rows 

                            //Get pixel 
                            //int pixel = get_pixel32( tile, xlp, ylp); 
                            //int prevpixel = get_pixel32( tileprev, xlp, ylp); 


                            //vram = 00000000   8 longs per tile
                            //       00000000
                            byte curnyb = 3-(xlp%4);
                            byte nybshift = (4*curnyb);
                            word curnybVal = (0xF)<<nybshift;
                            
                            word pixel = (vRam[vRamOffset+(xlp/4)+(ylp*2)] & curnybVal)>>nybshift;
                            word prevpixel = (vRam[vRamOffsetPrev+(xlp/4)+(ylp*2)] & curnybVal)>>nybshift;
                            //Copy pixel 
                            if( ( flipy==1 ) && ( flipx==1 ) )
                            {
                                desty=tiley+ry;
                                destx=tilex+rx+(hscrollpixoffset);
                                
                                //put_pixel32( flippedTile, rx, ry, colorBuf[line][pixel&0x000F]);
                                if (desty>0&&destx>0&&destx<960&&desty<720 && ((pixel&0x000F)!=0))
                                {
                                   //put_pixel32(scr,destx,desty,colorBuf[line][pixel&0x000F]);
                                   setpixelbuf(pixels, destx, desty, colorBuf[line][pixel&0x000F], true);
                                }
                                if (x==0)
                                {
                                    destxprev=(hscrollpixoffset)-rx;
                                    destyprev=desty;  
                                    if (destyprev>0&&destxprev>0&&destxprev<960&&destyprev<720&& ((prevpixel&0x000F)!=0))
                                    {
                                        //put_pixel32(scr,destxprev,destyprev,colorBuf[line][pixel&0x000F]);
                                        //setpixelbuf(pixels, destxprev, destyprev, colorBuf[line][prevpixel&0x000F], true);
                                    }
                                }
                            }
                            else 
                            {
                                 if( flipx==1 ) 
                                 {
                                     desty=tiley+ylp;
                                     destx=tilex+rx+(hscrollpixoffset);
                                     destxprev=(hscrollpixoffset)-rx;
                                     destyprev=desty;  
                                     //put_pixel32( flippedTile, rx, ylp, colorBuf[line][pixel&0x000F]);
                                     if (desty>0&&destx>0&&destx<960&&desty<720 && ((pixel&0x000F)!=0))
                                     {
                                         //put_pixel32(scr,destx,desty,colorBuf[line][pixel&0x000F]);
                                         setpixelbuf(pixels, destx, desty, colorBuf[line][pixel&0x000F], true);
                                     } 
                                     if (x==0)
                                     {
                                         if (destyprev>0&&destxprev>0&&destxprev<960&&destyprev<720&& ((prevpixel&0x000F)!=0))
                                         {
                                             //put_pixel32(scr,destxprev,destyprev,colorBuf[line][pixel&0x000F]);
                                             //setpixelbuf(pixels, destxprev, destyprev, colorBuf[line][prevpixel&0x000F], true);
                                         }
                                     }
                                 }
                                 else if( flipy==1 )
                                 { 
                                     desty=tiley+ry;
                                     destx=tilex+xlp+(hscrollpixoffset);
                                     destxprev=(hscrollpixoffset)-xlp;
                                     destyprev=desty;
                                       
                                     //put_pixel32( flippedTile, xlp, ry, colorBuf[line][pixel&0x000F]);
                                     if (desty>0&&destx>0&&destx<960&&desty<720 && ((pixel&0x000F)!=0))
                                     {
                                         //put_pixel32(scr,destx,desty,colorBuf[line][pixel&0x000F]);
                                         setpixelbuf(pixels, destx, desty, colorBuf[line][pixel&0x000F], true);
                                     } 
                                     if (x==0)
                                     {
                                         if (destyprev>0&&destxprev>0&&destxprev<960&&destyprev<720&& ((prevpixel&0x000F)!=0))
                                         {
                                             //put_pixel32(scr,destxprev,destyprev,colorBuf[line][pixel&0x000F]);
                                             //setpixelbuf(pixels, destxprev, destyprev, colorBuf[line][prevpixel&0x000F], true);
                                         }
                                     }
                                 }
                                 else
                                 { 
                                     desty=tiley+ylp;
                                     destx=tilex+xlp+(hscrollpixoffset);
                                     //put_pixel32( flippedTile, xlp, ylp, colorBuf[line][pixel&0x000F]);
                                     if (desty>0&&destx>0&&destx<960&&desty<720&& ((pixel&0x000F)!=0))
                                     {
                                         //put_pixel32(scr,destx,desty,colorBuf[line][pixel&0x000F]);
                                         setpixelbuf(pixels, destx, desty, colorBuf[line][pixel&0x000F], true);
                                     } 
                                     if (x==0)
                                     {
                                        destxprev=(hscrollpixoffset)-xlp;
                                        destyprev=desty;
                                        if (destyprev>0&&destxprev>0&&destxprev<960&&destyprev<720&& ((prevpixel&0x000F)!=0))
                                        {
                                             //put_pixel32(scr,destxprev,destyprev,colorBuf[line][pixel&0x000F]);
                                             //setpixelbuf(pixels, destxprev, destyprev, colorBuf[line][prevpixel&0x000F], true);
                                        }
                                     }
                                     
                                     //if (destyprev>0&&destxprev>0&&destxprev<960&&destyprev<720)
                                     //put_pixel32(scr,destxprev,destyprev,colorBuf[line][prevpixel&0x000F]);
                                 }
                            }
                         }
                    }
         }
     }
     */ 
